﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DataAccess;
using DataAccess.Exceptions;
using Model;

namespace Business.WEB
{
    public static class StudentRegistrationManager
    {
        public static Student ModifyStudent(string name, string lastname, string mlastname, string sex,
                                  string birthplace, DateTime birthday, string issuedIn, string liceAero,
                                  string profession, string address, string zone, string telephone, string email,
                                  int id, string fName, string fProfession, string mName, string mProfession, string observations)
        {
            try
            {
                if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(lastname) || String.IsNullOrEmpty(mlastname) || String.IsNullOrEmpty(sex) ||
                    String.IsNullOrEmpty(birthplace) || String.IsNullOrEmpty(issuedIn) || String.IsNullOrEmpty(liceAero) || String.IsNullOrEmpty(profession) ||
                    String.IsNullOrEmpty(address) || String.IsNullOrEmpty(zone) || String.IsNullOrEmpty(telephone) || String.IsNullOrEmpty(email) || String.IsNullOrEmpty(fName) ||
                    String.IsNullOrEmpty(fProfession) || String.IsNullOrEmpty(mName) || String.IsNullOrEmpty(mProfession) || String.IsNullOrEmpty(observations))
                    throw new ValidationException("Llenar todos los campos");

                string emailPattern = @"^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                             @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";

                if (!Regex.IsMatch(email, emailPattern))
                    throw new BusinessException("El email es incorrecto!");

                Student student = new Student
                {
                    name = name,
                    lastname = lastname,
                    mlastname = mlastname,
                    sex = sex,
                    birthday = birthday,
                    birthplace = birthplace,
                    id = id,
                    issuedIn = issuedIn,
                    liceAero = liceAero,
                    profession = profession,
                    address = address,
                    zone = zone,
                    telephone = telephone,
                    email = email,
                    fName = fName,
                    fProfession = fProfession,
                    mName = mName,
                    mProfession = mProfession,
                    observations = observations

                };
                student = StudentDataAccess.ModifyStudent(student);
                return student;
            }
            catch (ValidationException)
            {
                throw new ValidationException("Llenar todos los campos.");
            }
            catch (BusinessException)
            {
                throw new BusinessException("El email es incorrecto.");
            }
        }

    }
}
